import { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import type { RootState } from '../redux/store';
import { changeCinemaList } from '../redux/features/cinemaSlice';
import { findCinemas } from '@/api/MaiZuo';

const useCinemaList = () => {
  const list = useSelector((state: RootState) => state.cinema.list);
  const cityId = useSelector((state: RootState) => state.city.id);
  const cityName = useSelector((state: RootState) => state.city.name);
  const dispatch = useDispatch();

  useEffect(() => {
    if (list.length === 0) {
      findCinemas(cityId).then((res: any) => {
        console.log('cinema findCinemas', res);
        dispatch(changeCinemaList(res.cinemas));
      });
    } else {
      console.log('cinema 缓存');
    }
    return () => {};
  }, [list.length, cityId, dispatch]);
  return { list, cityName };
};

export { useCinemaList };
